var express = require('express');
var router = express.Router();
const { MatchModel } = require('../model/match.js');

router.get('/', (req, res)=> {
    MatchModel.find({}).then(matchs => {
        setTimeout(() => {
            console.log(matchs);
            res.json(matchs);
        }, 1000);
    });
});

router.post('/create', (req, res)=> {
    console.log(`nodelName: ${Match.modelName}`);
    const matchData = new MatchModel({
        status: "2",
        home:{
            name: "老同学", icon: "https://ts2.tc.mm.bing.net/th/id/OSB.iACK1IFXXDNVcL9vAy568w--.png?w=60&h=60&c=6&qlt=90&o=6&dpr=2&pid=BingSports", score: 0
        },
        away: {
            name: "龙源", icon: "https://ts3.tc.mm.bing.net/th/id/OSB.VXxjrqWZWwmw2Ju4T8AJJA--.png?w=60&h=60&c=6&qlt=90&o=6&dpr=2&pid=BingSports", score: 3
        },
        league: {
            name: "友谊赛", icon: "https://ts2.tc.mm.bing.net/th/id/OSB.hEhM6WADcDogo9SjjiSqPg--.png?w=50&h=50&c=6&qlt=90&o=6&dpr=2&pid=BingSports"}, status: "1"
        });
    matchData.save().then((result)=> {
        res.status(201).json(result);
    }).catch(err=> {
        console.error(err);
        res.status(500).send('Error creating match');
    });
});

module.exports = router;